//
//  ELCImagePickerController.m
//  ELCImagePickerDemo
//
//  Created by Collin Ruffenach on 9/9/10.
//  Copyright 2010 ELC Technologies. All rights reserved.
//

#import "ELCImagePickerController.h"
#import "ELCAsset.h"
#import "ELCAssetCell.h"
#import "ELCAssetTablePicker.h"
#import "ELCAlbumPickerController.h"

#import "PublicMethodEngine.h"
#import "SDDataCache.h"

@implementation ELCImagePickerController

@synthesize delegate;
@synthesize appendKey;

- (id)initWithRootViewController:(UIViewController *)rootViewController
{
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        NSDate *date = [NSDate date];
        NSTimeInterval time = [date timeIntervalSince1970];
        appendKey = [[NSString alloc] initWithFormat:@"%f", time];
        
        CLog(@"append key %@", appendKey);
    }
    
    return self;
}


-(void)cancelImagePicker {
	if([delegate respondsToSelector:@selector(elcImagePickerControllerDidCancel:)]) {
		[delegate performSelector:@selector(elcImagePickerControllerDidCancel:) withObject:self];
	}
}

-(void)selectedAssets:(NSArray*)_assets {

	NSMutableArray *returnArray = [[[NSMutableArray alloc] init] autorelease];
	
    //NSLog(@"assets %@", _assets);
    
	for(ALAsset *asset in _assets) {

        CLog(@"asset %@", asset);
        
		NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
		[workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:@"type"];
        //[workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
        
        
//        NSString *urlStr = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];
//        
//        UIImage *fullImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
//        
//        NSData *imageData = [[PublicMethodEngine sharedPublicMethodEngine] compresImageDataWith:fullImage maxPix:PHOTO_MAXPIX];
//        
//        [[SDDataCache sharedDataCache:kImageCache_Locale_Album] storeData:imageData forKey:urlStr];
//        
//        CLog(@"full image retain count %d", [fullImage retainCount]);
//        CLog(@"image data retain count %d", [imageData retainCount]);
//        
//        [fullImage release];
//        [imageData release];
        
        
		
        NSURL *url = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];
        NSString *urlStr = [NSString stringWithFormat:@"%@%@", appendKey, [url absoluteString]];
        [workingDictionary setObject:urlStr forKey:@"url"];
        
        //HOBO
        [workingDictionary setObject:[asset valueForProperty:ALAssetPropertyDate] forKey:@"date"];
		
		[returnArray addObject:workingDictionary];
		
		[workingDictionary release];	
	}
	
    [self popToRootViewControllerAnimated:NO];
    [[self parentViewController] dismissModalViewControllerAnimated:YES];
    
	if([delegate respondsToSelector:@selector(elcImagePickerController:didFinishPickingMediaWithInfo:)]) {
		[delegate performSelector:@selector(elcImagePickerController:didFinishPickingMediaWithInfo:) withObject:self withObject:[NSArray arrayWithArray:returnArray]];
	}
}

- (NSString *)getAppendKey
{
    return appendKey;
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {    
    NSLog(@"ELC Image Picker received memory warning.");
    
    [super didReceiveMemoryWarning];
}

- (void)viewDidLoad
{

    
    [super viewDidLoad];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}


- (void)dealloc {

    [appendKey release];
    CLog(@"ELCImagePickerController Picker dealloc");
    [super dealloc];
}

@end
